package demoxsgl_300.com.gouwu1500.zhi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demoxsgl_300.com.gouwu1500.R;
import demoxsgl_300.com.gouwu1500.adapter.BanSui_Adapter;
import demoxsgl_300.com.gouwu1500.bean.Bean;

/**
 * 伴随病症
 */
public class BanSuiBingZhengActivity extends Activity {

    @BindView(R.id.textView5)
    TextView textView5;
    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.imageView3)
    ImageView imageView3;
    @BindView(R.id.lv_list)
    ListView lvList1;
    @BindView(R.id.next_1)
    Button next1;
    List<Bean.BansuiBean> fazuoteidan;
    BanSui_Adapter banSui_adapter, banSui_adapter2;
    @BindView(R.id.re1)
    RelativeLayout re1;
    @BindView(R.id.lv_list2)
    ListView lvList2;
    @BindView(R.id.re2)
    RelativeLayout re2;
    @BindView(R.id.fanhui)
    TextView fanhui;
    private TagFlowLayout mFlowLayoutHistory, mFlowLayoutAll;
    LayoutInflater inflater;
    List<String> ccList1 = new ArrayList<String>();
    List<String> ccList2 = new ArrayList<String>();
    List<String> ccList3 = new ArrayList<String>();
    List<String> ccList4 = new ArrayList<String>();
    private TagAdapter<String> mAdapter ,mAdapter2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitybansuibingzheng);
        mFlowLayoutHistory = (TagFlowLayout) findViewById(R.id.id_flowlayout);
        mFlowLayoutAll = (TagFlowLayout) findViewById(R.id.id_flowlayout2);
        inflater = LayoutInflater.from(this);


        ButterKnife.bind(this);
        lvList1.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        banSui_adapter = new BanSui_Adapter(lvList1);
        fazuoteidan = Content.bean.getBansui();

        if (fazuoteidan != null && fazuoteidan.size() > 0) {
            if (fazuoteidan.size() >= 5) {
                for (int i = 0; i < 5; i++) {
                    banSui_adapter.addItem(fazuoteidan.get(i));
                }


            } else {
                banSui_adapter.addItem(fazuoteidan);
            }

        } else {
            Toast.makeText(this, "暂无数据", Toast.LENGTH_SHORT).show();
        }
        lvList1.setAdapter(banSui_adapter);
        lvList1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Bean.BansuiBean bansuiBean = fazuoteidan.get(position);
                List<Bean.BansuiBean.ValueBean> value = bansuiBean.getValue();
                //存在的元素
                ccList1.clear();




                for (int i = 0; i < value.size(); i++) {
                    ccList1.add(value.get(i).getName());
                }
                Log.e("111", "添加新"+ Arrays.toString(ccList1.toArray()));
                List<String> xiangTong = getXiangTong(ccList1, ccList3);
                Log.e("111", "选中的"+ Arrays.toString(ccList3.toArray()));
                Log.e("111", "相同"+ Arrays.toString(xiangTong.toArray()));
                Set<Integer> huoquweizhi = huoquweizhi(ccList1, xiangTong);
                mFlowLayoutHistory.setAdapter(mAdapter=new TagAdapter<String>(ccList1) {
                    @Override
                    public View getView(FlowLayout parent, int position, String s) {
                        TextView tv = (TextView) inflater.inflate(R.layout.tv,
                                mFlowLayoutHistory, false);
                        tv.setText(s);
                        return tv;
                    }
                });
                mAdapter.setSelectedList(huoquweizhi);

                mFlowLayoutHistory.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                    @Override
                    public boolean onTagClick(View view, int position, FlowLayout parent) {

                        if (!ccList3.contains(ccList1.get(position))) {
                            ccList3.add(ccList1.get(position));
//                            Toast.makeText(BanSuiBingZhengActivity.this, "添加", Toast.LENGTH_SHORT).show();
                        } else {
//                            Toast.makeText(BanSuiBingZhengActivity.this, "删除", Toast.LENGTH_SHORT).show();
                            shanchu(ccList3, ccList1.get(position));
                        }


                        //view.setVisibility(View.GONE);
                        return true;
                    }
                });
                banSui_adapter.setChecked(position);
                banSui_adapter.notifyDataSetChanged();
            }
        });
        get2();
    }

    public void get2() {
        lvList2.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        banSui_adapter2 = new BanSui_Adapter(lvList2);
        fazuoteidan = Content.bean.getBansui();

        if (fazuoteidan != null && fazuoteidan.size() > 0) {
            if (fazuoteidan.size() >= 10) {
                for (int i = 6; i < 10; i++) {
                    banSui_adapter2.addItem(fazuoteidan.get(i));
                }


            } else {
                for (int i = 6; i < fazuoteidan.size(); i++) {
                    banSui_adapter2.addItem(fazuoteidan.get(i));
                }
            }

        } else {
            Toast.makeText(this, "暂无数据", Toast.LENGTH_SHORT).show();
        }
        lvList2.setAdapter(banSui_adapter2);
        lvList2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Bean.BansuiBean bansuiBean = fazuoteidan.get(position + 5);
                List<Bean.BansuiBean.ValueBean> value = bansuiBean.getValue();
                ccList2.clear();
                for (int i = 0; i < value.size(); i++) {
                    ccList2.add(value.get(i).getName());
                }
                Log.e("111", "添加新"+ Arrays.toString(ccList2.toArray()));
                List<String> xiangTong = getXiangTong(ccList2, ccList4);
                Log.e("111", "选中的"+ Arrays.toString(ccList4.toArray()));
                Log.e("111", "相同"+ Arrays.toString(xiangTong.toArray()));
                Set<Integer> huoquweizhi = huoquweizhi(ccList2, xiangTong);
                mFlowLayoutAll.setAdapter(mAdapter2=new TagAdapter<String>(ccList2) {
                    @Override
                    public View getView(FlowLayout parent, int position, String s) {
                        TextView tv = (TextView) inflater.inflate(R.layout.tv,
                                mFlowLayoutAll, false);
                        tv.setText(s);
                        return tv;
                    }
                });
                mAdapter2.setSelectedList(huoquweizhi);
                mFlowLayoutAll.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                    @Override
                    public boolean onTagClick(View view, int position, FlowLayout parent) {
                        Toast.makeText(BanSuiBingZhengActivity.this, ccList2.get(position), Toast.LENGTH_SHORT).show();
                        if (!ccList4.contains(ccList2.get(position))) {
                            ccList4.add(ccList2.get(position));
//                            Toast.makeText(BanSuiBingZhengActivity.this, "添加", Toast.LENGTH_SHORT).show();
                        } else {
//                            Toast.makeText(BanSuiBingZhengActivity.this, "删除", Toast.LENGTH_SHORT).show();
                            shanchu(ccList4, ccList2.get(position));
                        }
                        return true;
                    }
                });
                banSui_adapter2.setChecked(position);
                banSui_adapter2.notifyDataSetChanged();
            }
        });
    }

    @OnClick({R.id.fanhui, R.id.next_1})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fanhui:
                finish();
                break;
            case R.id.next_1:
                Content.BanSui=Arrays.toString(ccList3.toArray())+","+Arrays.toString(ccList4.toArray());
                Content.BanSui=  Content.BanSui .replace("[","").replace("]","");
                startActivity(new Intent(this, FenXiActivity.class));
                finish();
                break;
        }
    }

    public void shanchu(List ccList1, String name) {
        for (int i = 0; i < ccList1.size(); i++) {
            if (name.equals(ccList1.get(i))) {
                ccList1.remove(i);
            }

        }
    }

    private static List<String> getXiangTong(List<String> list1, List<String> list2) {
        List<String> result = new ArrayList<>();
        for (String integer : list2) {
            if (list1.contains(integer)) {
                result.add(integer);
            }
        }
        return result;
    }

    Set<Integer> ccListInteger =  new HashSet();

    public Set<Integer> huoquweizhi(List Zhu, List zi) {
        ccListInteger.clear();
        for (int i = 0; i < zi.size(); i++) {
            if (Zhu.contains(zi.get(i))) {
                int iz = Zhu.indexOf(zi.get(i));
                ccListInteger.add(iz);
            }
        }
        return ccListInteger;

    }

}
